package cn.com.dealme.domain.model;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import cn.com.dealme.domain.model.exception.CatagoryException;
import cn.com.dealme.domain.vo.CatagoryVO;

public class Catagory {

	private Long id;

	private CatagoryVO attributes;

	private Set<Catagory> subCatagorySet;

	private Catagory parent;
	
	private PublishMode publishMode;

	public Catagory() {
	}
	
	public Catagory(Long id) {
		this.id = id;
	}

	public Catagory(CatagoryVO attributes, Set<Catagory> subCatagoryList) {
		if (null == attributes) {
			attributes = newAttributes();
		}
		if (null == subCatagoryList) {
			subCatagoryList = new HashSet<Catagory>();
		}
		this.attributes = attributes;
		for (Catagory c : subCatagoryList) {
			c.setParent(this);
		}
		this.subCatagorySet = subCatagoryList;
	}

	public Catagory(CatagoryVO attributes, Catagory parent) {
		if (null == attributes) {
			attributes = newAttributes();
		}
		this.attributes = attributes;
		this.parent = parent;
	}

	private CatagoryVO newAttributes() {
		return new CatagoryVO();
	}

	/** action **/

	public void update(CatagoryVO attributes) {
		this.attributes.setName(attributes.getName());
	}
	
	public void setPublishMode(PublishMode publishMode) {
		this.publishMode = publishMode;
	}
	
	public void setParent(Catagory parent) {
		if (!subable()) {
			throw new CatagoryException(CatagoryException.NOT_SUB_EXCEPTION);
		}
		this.parent = parent;
	}

	public void addSubCatagory(Catagory sub) {
		if (!topable()) {
			throw new CatagoryException(CatagoryException.NOT_TOP_EXCEPTION);
		}
		sub.setParent(this);
		this.subCatagorySet.add(sub);
	}

	public void removeSubCatagory(Catagory sub) {
		if (!topable()) {
			throw new CatagoryException(CatagoryException.NOT_TOP_EXCEPTION);
		}
		sub.setParent(null);
		this.subCatagorySet.remove(sub);
	}

	public void removeSubCatagory(Collection<Catagory> subCollection) {
		if (!topable()) {
			throw new CatagoryException(CatagoryException.NOT_TOP_EXCEPTION);
		}
		for (Catagory c : subCollection) {
			c.setParent(null);
		}
		this.subCatagorySet.remove(subCollection);
	}

	
	public boolean subable() {
		if (null == subCatagorySet || subCatagorySet.size() == 0) {
			return true;
		}
		return false;
	}

	public boolean topable() {
		if(null == parent) return true;
		return false;
	}

	/** readers and setters **/

	public Long getId() {
		return id;
	}

	public CatagoryVO getAttributes() {
		return attributes;
	}

	public Set<Catagory> getSubCatagorySet() {
		return subCatagorySet;
	}

	public Catagory getParent() {
		return parent;
	}

	public PublishMode getPublishMode() {
		return publishMode;
	}

	

	protected void setId(Long id) {
		this.id = id;
	}

	protected void setAttributes(CatagoryVO attributes) {
		this.attributes = attributes;
	}

	protected void setSubCatagorySet(Set<Catagory> subCatagoryList) {
		this.subCatagorySet = subCatagoryList;
	}

	@Override
	public boolean equals(Object obj) {
		if(null == obj) return false;
		if(!(obj instanceof Catagory)) return false;
		if(this.id.equals(((Catagory)obj).id)) {
			return true;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.id.hashCode();
	}

	
}
